package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.pojo.Inventory;
import com.zhentao.service.InventoryAdjustService;
import com.zhentao.service.IDishCategoryService;
import com.zhentao.service.InventoryService;
import com.zhentao.util.Result;
import org.example.dish_pojo.DishCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

@RestController
@RequestMapping("kucun")
public class KucunCaiPinController {
    @Autowired
    InventoryService service;
    @Autowired
    IDishCategoryService iDishCategoryService;
    @Autowired
    private InventoryAdjustService adjustService;

    @RequestMapping("findall")
    public Result findall(@RequestBody Inventory inventory) {
        return service.findainven(inventory);
    }

    @RequestMapping("findtype")
    public Result findtype() {
        List<DishCategory> list = iDishCategoryService.list();
        return Result.OK(list);
    }


    // 添加库存
    @RequestMapping("/addInventory")
    public Result addInventory(@RequestBody Inventory inventory) {
        boolean save = service.save(inventory);
        if (!save) {
            return new Result(500, "添加失败", null);
        }
        return new Result(200, "添加成功", null);
    }

    //修改库存
    @RequestMapping("/updateInventory")
    public Result updateInventory(@RequestBody Inventory inventory) {
        boolean update = service.updateById(inventory);
        if (!update) {
            return new Result(500, "修改失败", null);
        }
        return new Result(200, "修改成功", null);
    }

    // 更新预警/上限阈值
    @RequestMapping("/updateThresholds")
    public Result updateThresholds(Integer inventoryId, Integer warningStock, Integer highThreshold){
        return service.updateThresholds(inventoryId, warningStock, highThreshold);
    }

    // 库存调整：根据 inventoryId 直接设置库存并记录调整记录
    @RequestMapping("/adjust")
    public Result adjust(Integer inventoryId, Integer newStock, String remark, Integer operatorId){
        return adjustService.adjust(inventoryId,newStock,remark,operatorId);
    }

    // 调整记录查询
    @RequestMapping("/adjustRecords")
    public Result adjustRecords(Integer dishId, Integer pageNum, Integer pageSize){
        return adjustService.listByDish(dishId,pageNum,pageSize);
    }

    // 导出
    @RequestMapping("/export")
    public void export(@RequestBody Inventory filter, HttpServletResponse response){
        service.export(filter, response);
    }


    // 删除库存,根据dishId删除
    // 删除库存,根据dishId删除
    @RequestMapping("/deleteInventory")
    public Result deleteInventory(Integer dishId) {
        // 根据dishId删除库存记录，而不是使用removeById方法
        QueryWrapper<Inventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dish_id", dishId);
        boolean delete = service.remove(queryWrapper);
        if (!delete) {
            return new Result(500, "删除失败", null);
        }
        return new Result(200, "删除成功", null);
    }
}
